package com.wing.product.controller;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.druid.util.StringUtils;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.common.annotation.RequestObjectFieldIgnore;
import com.wing.common.constant.SecurityConstants;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.PageResult;
import com.wing.product.model.entity.WarehouseMoveInventory;
import com.wing.product.model.entity.WarehouseProduct;
import com.wing.product.model.entity.WarehouseProductInventory;
import com.wing.product.model.entity.WarehouseProductionLine;
import com.wing.product.model.request.WarehouseProductForm;
import com.wing.product.model.response.MoveInventoryVO;
import com.wing.product.model.response.WarehouseProductInventoryVO;
import com.wing.product.model.response.WarehouseProductVO;
import com.wing.product.service.WarehouseMoveInventoryService;
import com.wing.product.service.WarehouseProductInventoryService;
import com.wing.web.utils.JwtUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.ss.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 产品入库表(warehouse_product_inventory)表控制层
 *
 * @author xxxxx
 */
@Api(value = "产品记录明细管理", tags = "#/warehouse/product/warehouseProductInventory")
@RestController("WarehouseProductInventoryController")
@RequestMapping("/warehouse/productInventory")
public class WarehouseProductInventoryController {
    /**
     * 服务对象
     */
    @Autowired
    private WarehouseProductInventoryService warehouseProductInventoryService;


    @Autowired
    private WarehouseMoveInventoryService warehouseMoveInventoryService;

    @ApiOperation(value = "查询产品明细出库", notes = "查询")
    @GetMapping("/selectProduct")
    public JsonResult<List<WarehouseProductInventoryVO>> selectProduct(String spaceId, String productId, @RequestParam(value = "bundleCodeList[]", required = false) List<String> bundleCodeList) {
        if (StringUtils.isEmpty(spaceId) || StringUtils.isEmpty(productId)) {
            return JsonResult.fail("产品id或库位id为空");
        } else {
            return warehouseProductInventoryService.selectProduct(spaceId, productId, bundleCodeList);
        }
    }

    @ApiOperation(value = "查询产品明细出库记录", notes = "查询")
    @GetMapping("/selectProductDetail")
    public List<WarehouseProductInventoryVO> selectProductDetail(String id) {
        return warehouseProductInventoryService.selectProductDetail(id);
    }


    @ApiOperation(value = "查询所有", notes = "查询")
    @GetMapping("/list")
    public PageResult<WarehouseProductInventoryVO> list(
            @RequestParam(value = "page", defaultValue = "1", required = false) Integer page
            , @RequestParam(value = "limit", defaultValue = "10", required = false) Integer limit
            , @RequestParam(value = "sort", defaultValue = "create_time", required = false) String sort
            , @RequestParam(value = "order", defaultValue = "asc", required = false) String order
            , @RequestParam(value = "notIds[]", required = false) List<Integer> notIds
            , Long customerId
            , String customerCode
            , String productName
            , String productVariety
            , Long productVarietyId
            , String productBrand
            , Long productBrandId
            , String productMark
            , Long productMarkId
            , String productProducer
            , Long productProducerId
            , String productSpecCode
            , String productCode
            , String userBundleCode
            , Long warehouseSignOrderId
            , Long productTypeId
            , String productStatus
            , String transferId) {
        if (JwtUtils.getClientId().equals(SecurityConstants.WAREHOUSE_CLIENT_ID)) {
            customerId = JwtUtils.getDepartmentId();
        }
        Page<WarehouseProduct> pg = new Page<>(page, limit);
        Map paramMap = new HashMap();
        if (!StringUtils.isEmpty(transferId)) {
            paramMap.put("transferId", transferId);
        }
        if (!ObjectUtil.isEmpty(warehouseSignOrderId)) {
            paramMap.put("warehouseSignOrderId", warehouseSignOrderId);
        }
        paramMap.put("customerId", customerId);
        paramMap.put("notIds", notIds);
        paramMap.put("productTypeId", productTypeId);
        paramMap.put("customerCode", customerCode);
        paramMap.put("productVariety", productVariety);
        paramMap.put("productVarietyId", productVarietyId);
        paramMap.put("productBrand", productBrand);
        paramMap.put("productBrandId", productBrandId);
        paramMap.put("productMark", productMark);
        paramMap.put("productMarkId", productMarkId);
        paramMap.put("productProducer", productProducer);
        paramMap.put("productProducerId", productProducerId);
        paramMap.put("productName", productName);
        paramMap.put("productSpecCode", productSpecCode);
        paramMap.put("productCode", productCode);
        paramMap.put("userBundleCode", userBundleCode);
        paramMap.put("productStatus", productStatus);
        IPage<WarehouseProductInventoryVO> list = warehouseProductInventoryService.list4app(pg, paramMap);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }

    @ApiOperation(value = "查询客户捆包码是否存在", notes = "查询")
    @PostMapping("/queryUserBundleCode")
    public JsonResult<WarehouseProductVO> queryUserBundleCode(@RequestBody List<String> userBundleCodes) {
        Integer count = warehouseProductInventoryService.queryUserBundleCode(userBundleCodes);
        return JsonResult.success(count);
    }


    @ApiOperation(value = "看板货主库存排行", notes = "搜索")
    @GetMapping("/selectRanking")
    public JsonResult<Map<String, Object>> selectRanking() {
        return warehouseProductInventoryService.selectRanking();
    }

    @ApiOperation(value = "看板库存量", notes = "搜索")
    @GetMapping("/selectInventorySum")
    public JsonResult<Map<Object, Object>> selectInventorySum() {
        return warehouseProductInventoryService.selectInventorySum();
    }

    @ApiOperation(value = "看板库存趋势", notes = "搜索")
    @GetMapping("/selectInventoryTrend")
    public JsonResult<Map<Object, Object>> selectInventoryTrend() {
        return warehouseProductInventoryService.selectInventoryTrend();
    }

    @ApiOperation(value = "查询产品明细领料", notes = "查询")
    @PostMapping("/selectMaterialDetail")
    public JsonResult<List<WarehouseProductInventoryVO>> selectMaterialDetail(@RequestBody List<String> materialNumberList) {
        if (!CollectionUtils.isEmpty(materialNumberList)) {
            List<WarehouseProductInventoryVO> warehouseProductVOList = warehouseProductInventoryService.selectMaterialDetail(materialNumberList);
            if (CollectionUtils.isNotEmpty(warehouseProductVOList)) {
                return JsonResult.success("请求成功", warehouseProductVOList);
            } else {
                return JsonResult.success("数据为空", warehouseProductVOList);
            }
        } else {
            return JsonResult.fail("id为空");
        }
    }

    @ApiOperation(value = "复核", notes = "单个签收")
    @GetMapping("/review")
    public JsonResult signSingle(Long id,Long signId) {
        return JsonResult.success(warehouseProductInventoryService.review(id, signId));
    }


    @ApiOperation(value = "查看产品详情", notes = "查看产品详情")
    @GetMapping("/selectByInventoryId")
    public JsonResult selectByInventoryId(String id) {
        return JsonResult.success(warehouseProductInventoryService.selectByInventoryId(id));
    }

    @ApiOperation(value = "修改", notes = "修改")
    @PostMapping("/update")
//    public JsonResult update(@RequestBody WarehouseProductInventoryVO warehouseProductInventoryVO)  {
    public JsonResult update(WarehouseProductInventoryVO warehouseProductInventoryVO)  {

        try{
            warehouseProductInventoryService.update(warehouseProductInventoryVO);
            return JsonResult.success("修改成功");
        }catch (Exception e){
            return JsonResult.fail(e.getMessage());
        }
    }

    @ApiOperation(value = "查看产品详情", notes = "查看产品详情")
    @GetMapping("/selectInventoryProcess")
    public JsonResult selectInventoryProcess(String id) {

        return JsonResult.success(warehouseProductInventoryService.selectInventoryProcess(id));
    }

}
